def set_stage():
""" Sets up the stage for the game """
stage.set_background("soccerfield")
stage.disable_floor()
def add_player():
""" Adds a player to the stage for the user to control """
player = codesters.Sprite("player1")
player.go_to(0, -180)
player.set_size(0.75)
return player
def add_ball():
""" Adds a ball to the stage and sets its attributes """
ball = codesters.Sprite("soccerball")
ball.set_y_speed(-8)
def head_ball(sprite, hit_sprite):
sprite.go_to(0, 0)
hit_sprite.hide()
# add any other actions...
def main():
""" Sets up the program and calls other functions """
set_stage()
player = add_player()
add_ball()
player.event_collision(head_ball)
main()
t = codesters.Teacher()
defs = t.find_block("def")
get_y_speeds = t.find_function("get_y_speed")
docstrings = t.find_text('"""')
num_docstrings = len(docstrings)
default_doc = '""" This is a description of what this function does """'.replace(' ','').lower()
hides = t.find_function("hide")
go_tos = t.find_function("go_to")
def get_above_def(line_number, def_list):
""" Returns the name of the nearest function and the distance above the given line number """
function_name = ""
distance = line_number - def_list[0][0]
for func in def_list:
if line_number - func[0] <= distance and line_number - func[0] > 0:
function_name = func[1]
distance = line_number - func[0]
return function_name, distance
def find_new_function(ignore_list, def_list):
""" Returns tuples of (line_number, function def) for any functions that aren't specified in ignore list """
return_list = []
for d in def_list:
count = 0
for i in ignore_list:
if i in d[1]:
break
else:
count += 1
if count == len(ignore_list):
return_list.append((d[0],d[1]))
return return_list
try:
docstr_text = docstrings[3][1]
docstr_text = docstr_text.replace(' ','').lower()
docstr_line_num = docstrings[3][0]
docstr_indent = t.get_indent_at_line(docstr_line_num)
except:
docstr_text = "DNE"
docstr_line_num == "DNE"
docstr_indent = "DNE"
try:
docstr_def, docstr_distance = get_above_def(docstr_line_num, defs)
except:
docstr_def = "DNE"
docstr_distance = "DNE"
try:
tval2 = get_y_speeds[0][1]
tval2_line_num = get_y_speeds[0][0]
tval2_indent = t.get_indent_at_line(tval2_line_num)
except:
tval2 = "DNE"
tval2_line_num = "DNE"
tval2_indent = "DNE"
try:
tval2_above_def, tval2_distance = get_above_def(tval2_line_num, defs)
except:
tval2_above_def = "DNE"
tval2_distance = "DNE"
tval3 = len(hides)
tval4 = len(go_tos)
t1 = TestObjective()
t1.add_success(num_docstrings > 4, "Great job!")
t1.add_failure(num_docstrings == 4, "Did you add a Docstring under head_ball()?")
t1.add_failure(num_docstrings < 0, "Oops. Did you delete a docstring?")
t2 = TestObjective()
t2.add_success(docstr_text != default_doc and num_docstrings > 4, "Great job!")
t2.add_failure(docstr_text == default_doc, "Did you change the docstring to describe the function?")
t2.add_failure(num_docstrings == 4, "Did you add a Docstring under head_ball()?")
t3 = TestObjective()
t3.add_success(docstr_indent == 4 and num_docstrings > 4, "Great job!")
t3.add_failure(docstr_indent < 4, "Did you indent the docstring inside head_ball()?")
t3.add_failure(docstr_indent > 4, "Oops, did you indent the docstring more than once?")
t3.add_failure(docstr_indent == "DNE", "Did you indent the docstring inside head_ball()?")
t3.add_failure(num_docstrings == 4, "Did you add a Docstring under head_ball()?")
t4 = TestObjective()
t4.add_success("head_ball" in docstr_def and docstr_distance < 5, "Great job!")
t4.add_failure("head_ball" not in docstr_def, "Did you place the docstring under head_ball()?")
t4.add_failure(docstr_distance >= 5, "Make sure the docstring is at the top of the function!")
t4.add_failure(docstr_def == "DNE", "Did you delete a docstring?")
t5 = TestObjective()
t5.add_success("hit_sprite" in tval2, "Great job!")
t5.add_failure(tval2 == "DNE", "Did you add Get y Speed into head_ball()?")
t5.add_failure(tval2 != "DNE" and "hit_sprite" not in tval2, "Did you change sprite to hit_sprite in front of .get_y_speed()?")
t6 = TestObjective()
t6.add_success("head_ball" in tval2_above_def and tval2_indent == 4, "Great job!")
t6.add_failure("head_ball" not in tval2_above_def, "Did you place Get y Speed within head_ball()?")
t6.add_failure(tval2_indent < 4 or tval2_indent > 4, "Make sure Get y Speed is indented within head_ball()!")
t6.add_failure(tval2_above_def == "DNE", "Did you add Get y Speed within head_ball()?")
t7 = TestObjective()
t7.add_success(tval3 == 0 and tval4 == 0, "Great job!")
t7.add_failure(tval3 > 0, "Did you delete the hide command inside of head_ball()?")
t7.add_failure(tval4 > 1, "Did you delete the go_to command inside of head_ball()?")
tester = TestManager()
tester.add_test_list([t1, t2, t3, t4, t5, t6, t7])
tester.run_tests()
tester.display_first_feedback()